API reference

API reference for TTP module.

class ttp.ttp(data='', template='', log_level='WARNING', log_file=None, base_path='', vars={})

Template Text Parser main class to load data, templates, lookups, variables and dispatch data to parser object to parse in single or multiple processes, construct final results and run outputs.

Parameters

  • data file object or OS path to text file or directory with text files with data to parse
  • template file object or OS path to text file with template
  • base_path (str) base OS path prefix to load data from for template’s inputs
  • log_level (str) level of logging “DEBUG”, “INFO”, “WARNING”, “ERROR”, “CRITICAL”
  • log_file (str) path where to save log file
  • vars dictionary of variables to make available to ttp parser

Example:

from ttp import ttp
parser = ttp(data="/os/path/to/data/dir/", template="/os/path/to/template.txt")
parser.parse()
result = parser.result(format="json")
print(result[0])
add_input(data, input_name='Default_Input', groups=['all'])

Method to load additional data to be parsed. This data will be used to fill in template input with input_name and parse that data against a list of provided groups.

Parameters

  • data file object or OS path to text file or directory with text files with data to parse
  • input_name (str) name of the input to put data in, default is Default_Input
  • groups (list) list of group names to use to parse this input data
add_lookup(name, text_data='', include=None, load='python', key=None)

Method to add lookup table data to all templates loaded so far. Lookup is a text representation of structure that can be loaded into python dictionary using one of the available loaders - python, csv, ini, yaml, json.

Parameters

  • name (str) name to assign to this lookup table to reference in templates
  • text_data (str) text to load lookup table/dictionary from
  • include (str) absolute or relative /os/path/to/lookup/table/file.txt
  • load (str) name of TTP loader to use to load table data
  • key (str) specify key column for csv loader to construct dictionary

include can accept relative OS path - relative to the directory where TTP will be invoked either using CLI tool or as a module

add_template(template, template_name=None)

Method to load TTP templates into the parser.

Parameters

  • template file object or OS path to text file with template
  • template_name (str) name of the template
add_vars(vars)

Method to add variables to ttp and its templates to reference during parsing

Parameters

  • vars dictionary of variables to make available to ttp parser
clear_input()

Method to delete all input data for all templates, can be used prior to adding new set of data to parse with same templates, instead of re-initializing ttp object.

get_input_commands_dict()

Method to iterate over all templates and inputs to get a list of commands that needs to be present in text data, that text data will be consumed by inputs, each input will extract output for its commands and will run groups parsing for it.

Returns

Dictionary of {“input_name”: [input commands list]}

get_input_commands_list()

Method to iterate over all templates and inputs to get a list of commands that needs to be present in text data, that text data will be consumed by inputs, each input will extract output for its commands and will run groups parsing for it.

Returns

List of unique commands - [command1, command2, … , commandN]

parse(one=False, multi=False)

Method to parse data with templates.

Parameters

  • one (boolean) if set to True will run parsing in single process
  • multi (boolean) if set to True will run parsing in multiprocess

By default one and multi set to False and TTP will run parsing following below rules:

  1. if one or multi set to True run in one or multi process
  2. if overall data size is less then 5Mbyte, use single process
  3. if overall data size is more then 5Mbytes, use multiprocess

In addition to 3 TTP will check if number of input data items more then 1, if so multiple processes will be used and one process otherwise.

result(templates=[], structure='list', returner='self', **kwargs)

Method to get parsing results, supports basic filtering based on templates’ names, results can be formatted and returned to specified returner.

Parameters

  • templates (list or str) names of the templates to return results for
  • returner (str) returner to use to return data - self, file, terminal
  • structure (str) structure type, valid values - list or dictionary

kwargs - can contain any attributes supported by output tags, for instance:

  • format (str) formatter name - yaml, json, raw, pprint, csv, table, tabulate
  • functions (str) reference functions to run results through

Example:

from ttp import ttp
parser = ttp(data="/os/path/to/data/dir/", template="/os/path/to/template.txt")
parser.parse()
json_result = parser.result(format="json")[0]
yaml_result = parser.result(format="yaml")[0]
print(json_result)
print(yaml_result)

Returns

By default template results set to per_input and structure set to list, returns list such as:

[
   [ template_1_input_1_results,
     template_1_input_2_results,
     ...
     template_1_input_N_results ],
   [ template_2_input_1_results,
     template_2_input_2_results,
     ...
]

If template results set to per_template and structure set to list, returns list such as:

[
   [ template_1_input_1_2...N_joined_results ],
   [ template_2_input_1_2...N_joined_results ]
]

If template results set to per_input and structure set to dictionary, returns dictionary such as:

{
   template_1_name: [
     input_1_results,
     input_2_results,
     ...
     input_N_results
    ],
   template_2_name: [
     input_1_results,
     input_2_results
    ],
     ...
}

If template results set to per_template and structure set to dictionary, returns dictionary such as:

{
   template_1_name: input_1_2...N_joined_results,
   template_2_name: input_1_2...N_joined_results
}
set_input(data, input_name='Default_Input', groups=['all'])

Method to replace existing templates data with new set of data. This method run clear_input first and add_input method after that.

Parameters

  • data file object or OS path to text file or directory with text files with data to parse
  • input_name (str) name of the input to put data in, default is Default_Input
  • groups (list) list of group names to use to parse this input data